"""
能切换视频播放不间断
"""
import sys 
from PySide2.QtMultimedia import *
from PySide2.QtMultimediaWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *



class SwitchVideo(QWidget):

    def __init__(self,width=960,height=1080) :
        super(SwitchVideo,self).__init__()
        self.setStyleSheet("border:0px")
        self.resize(QSize(width,height))
        self.main_layout = QVBoxLayout()
        self.main_layout.setMargin(0)
        self.mediaPlayer = QMediaPlayer(None,QMediaPlayer.VideoSurface)

        self.mediaPlayer.durationChanged.connect(self._duration_changed)
        self.mediaPlayer.positionChanged.connect(self._position_changed)
        self.videoWidget = QVideoWidget()
        self.mediaPlayer.setVideoOutput(self.videoWidget)
        # self.set_video( r'C:\Users\Admin\Downloads\Video\A.mp4')
        # self.videoWidget.setFixedSize(QSize(960,1080))
        self.videoWidget.resize(QSize(width,height))
        self.main_layout.addWidget(self.videoWidget)
        self.setLayout(self.main_layout)

        self.duration = 0

    def _duration_changed(self,duration:int):
        """
        槽函数，当播放时间改变时
        """
        print('duration:',duration)
        self.duration = duration

    def _position_changed(self,position:int):
        """
        当播放位置更改时
        """
        print('position:',position)
        # show_length = position*(200.0/self.duration)
        # print('show_length:',show_length)
        # self.progress.setFixedWidth(show_length)

    def set_video(self,video_path):
        self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(video_path)))
        self.mediaPlayer.play()